package com.mryang.hds.controller;

import com.mryang.hds.entity.Need;
import com.mryang.hds.service.INeedService;
import com.mryang.hds.utils.DateUtil;
import com.mryang.hds.utils.JWTUtil;
import com.mryang.hds.utils.ResultJson;
import io.minio.credentials.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName NeedController.java
 * @Description TODO
 * @createTime 2023年03月20日 12:24:00
 */

@RestController
@RequestMapping("/api/need")
public class NeedController {

    @Autowired
    INeedService iNeedService;

    @PostMapping("/needsub")
    private ResultJson needSub(Need need, HttpServletRequest httpServletRequest){

        // 获取当前登录用户编号
        String token = httpServletRequest.getHeader("token");
        Integer userId = JWTUtil.getTokenInfoIndex(token);
        need.setUserId(userId);

        // 获取提交时间
        String time = DateUtil.dateToString(new Date(),null);
        need.setNeedTime(time);

        // 设置需求状态
        need.setNeedState(0);

        // System.out.println(need);

        iNeedService.needSub(need);

        return ResultJson.success("需求提交成功");

    }

    // 获取距离现在最近的一条记录信息
    @GetMapping("/getNeedNow")
    private ResultJson getNeedNow(HttpServletRequest httpServletRequest){

        Integer loginId = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));

        Need oneNeedByUId = iNeedService.getOneNeedByUId(loginId);

        return ResultJson.success(oneNeedByUId);

    }
}
